package com.hj.aimin.auth.Controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.hj.aimin.auth.Service.WxService;
import com.hj.aimin.auth.common.Result;
import com.hj.aimin.auth.pojo.Jscode2sessionResult;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/public/wx")
@RequiredArgsConstructor
public class WxController {

    private final WxService wxService;

    @GetMapping("/token")
    public Result<?> token(String code) {
        Jscode2sessionResult result = wxService.wxLogin(code);
        //数据库操作
        StpUtil.login(1);
        SaTokenInfo  tokenInfo = StpUtil.getTokenInfo();
        String  tokenName = tokenInfo.getTokenName();
        String  tokenValue = tokenInfo.getTokenValue();
        if (tokenValue == null||  tokenName == null) {
            return Result.error(500, "登录失败");
        }
        return Result.success(tokenInfo);
    }
    @GetMapping("/logout")
    public Result<?> logout() {
        try {
            StpUtil.logout();
            return Result.success("登出成功");
        } catch (Exception e) {
            return Result.error(500, "登出失败");
        }

    }
    @GetMapping("/checkLogin")
    public Result<?> checkLogin() {
       try  {
           StpUtil.checkLogin();
           return Result.success("已登录");
       } catch (Exception e) {
           return Result.error(500, "未登录");
       }
    }
}
